package com.duan.boot.controller;

import com.duan.boot.common.R;
import com.duan.boot.entity.Department;
import com.duan.boot.entity.User;
import com.duan.boot.service.DepService;
import com.duan.boot.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


/**
 * @author duan
 */
@Data
@RestController
@RequestMapping("/v1/oa/department")
@Api("部门操作")
public class DepController {
    @Autowired
    private DepService depService;

    @Autowired
    private UserService userService;

    @ApiOperation("增加一个部门")
    @RequestMapping(value = "/changeDep/addDep", method = RequestMethod.POST)
    public R adddep(@RequestParam(value = "depName") String depName, @RequestParam(value = "depDesc") String depDesc){
        Department department = new Department();
        department.setDepName(depName);
        department.setDepDesc(depDesc);
        return R.ok(depService.insert(department));
    }

    @ApiOperation("根据id删除一个部门")
    @RequestMapping(value = "/changeDep/remove", method = RequestMethod.DELETE)
    public R remove(@RequestParam Integer depId){
        return R.ok(depService.delete(depId));
    }

    @ApiOperation("修改某一个部门")
    @RequestMapping(value = "/changeDep/save", method = RequestMethod.PUT)
    public R save(@RequestParam Integer desId,
                  @RequestParam String desName,
                  @RequestParam String desDesc){
        Department department = new Department();
        department.setDepId(desId);
        department.setDepName(desName);
        department.setDepDesc(desDesc);
        return depService.upDate(department) ? R.ok(true) : R.fail("请确定输入的部门id存在且正确");
    }

    @ApiOperation("查找所有的部门")
    @RequestMapping(value = "/changeDep/findAll", method = RequestMethod.GET)
    public R findAll(){
        return R.ok(depService.findAll());
    }

    @ApiOperation("根据id获得部门的详细信息")
    @RequestMapping(value = "/changeDep/getById/{depId}", method = RequestMethod.GET)
    public R getById(@PathVariable int depId){
        return depService.getDepById(depId)==null ? R.fail("没有该部门"):R.ok(depService.getDepById(depId));
    }

    @ApiOperation("根据部门名称获取详细信息")
    @RequestMapping(value = "/changeDep/getByName/{depName}", method = RequestMethod.GET)
    public R getByName(@PathVariable String depName){
        return depService.getDepByName(depName)==null ? R.fail("没有该部门"): R.ok(depService.getDepByName(depName));
    }

    @ApiOperation("根据用户所在的部门获得信息")
    @RequestMapping(value = "/changDep/getWithUser",  method = RequestMethod.GET)
    public R getWithUser(@RequestParam Integer userId, @RequestParam String userName){
        User user = new User();
        user.setUserId(userId);
        user.setUserName(userName);
        if (depService.getWithUser(user)==null){
            return R.fail("请确定您想要查找的成员存在");
        }else {
            return R.ok(depService.getWithUser(user));
        }
    }

    @ApiOperation("增加一个用户")
    @RequestMapping(value = "/changeUser/adduser", method = RequestMethod.POST)
    public R adduser(@RequestBody @Valid User user){
        return R.ok(depService.adduser(user));
    }

    @ApiOperation("根据部门名称显示部门所有的成员")
    @RequestMapping(value = "/changeUser/showuser/{depName}", method = RequestMethod.GET)
    public R showUser(@PathVariable String depName){
        return R.ok(depService.showUser(depName));
    }

    @ApiOperation("根据用户的学号来删除他的信息")
    @RequestMapping(value = "/changeUser/removeUser/{userId}", method = RequestMethod.DELETE)
    public R removeUser(@PathVariable int userId){
        return R.ok(depService.removeUser(userId));
    }

    @ApiOperation("修改已经存在的用户的信息")
    @RequestMapping(value = "/changeUser/saveUser", method = RequestMethod.PUT)
    public R saveUser(@RequestBody User user){
        return R.ok(depService.saveUser(user));
    }

    @ApiOperation("根据部门id或者部门名称查找部门")
    @RequestMapping(value = "/changeDep/searchDep", method = RequestMethod.GET)
    public R searchDep(@RequestParam Object idOrName){
        if (depService.searchDep(idOrName)==null){
            return R.fail("没有查询到信息,请确认你的信息是正确的");
        }else {
            return R.ok(depService.searchDep(idOrName));
        }
    }

    @ApiOperation("直接查找用户的信息")
    @RequestMapping(value = "/changeUser/findUser", method = RequestMethod.GET)
    public R findUser(@RequestParam String userName){
        if (userService.selectUserByName(userName)==null){
            return R.fail("这里没有该用户，请确认你想查找的用户存在");
        }else {
            return R.ok(userService.selectUserByName(userName));
        }
    }

    @ApiOperation("根据id或者name查找用户的信息")
    @RequestMapping(value = "/changeUser/findUserByUserIdOrName", method = RequestMethod.GET)
    public R findUserByUserIdOrName(@RequestParam Object idOrName){
        return depService.findUserByIdOrName(idOrName) == null ? R.fail("您查找的用户不存在") : R.ok(depService.findUserByIdOrName(idOrName));
    }

    @ApiOperation("查找部门和他对应的成员")
    @RequestMapping(value = "/changeUser/findDepAndUser", method = RequestMethod.GET)
    public R findDepAndUser(){
        return R.ok(depService.findDepAndUser());
    }

    @ApiOperation("查找所有部门的人数")
    @RequestMapping(value = "/changeUser/numberOfDep", method = RequestMethod.GET)
    public R numberOfDep(@RequestParam Integer depId){
        return R.ok(depService.numberOfDep(depId));
    }

    @ApiOperation("分页查询")
    @RequestMapping(value = "/changeUser/pagedQuery", method = RequestMethod.GET)
    public R pagedQuery(@RequestParam Integer page,
                        @RequestParam Integer numberOfPage,
                        @RequestParam Integer depId){
        return R.ok(depService.pagedQuery(page,numberOfPage, depId));
    }

    @ApiOperation("分页查询页码个数")
    @RequestMapping(value = "/changeUser/pagedQueryPage", method = RequestMethod.GET)
    public R pagedQueryPage(@RequestParam Integer numberOfPage,
                            @RequestParam Integer depId){
        return R.ok(depService.pagedQueryPage(numberOfPage, depId));
    }
}
